package com.ljb.demo2;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;


/**
 * Created by ljb on 2017/7/20.
 */

public class CameraTest extends Activity {
    Context context = this;
    private static final String TAG = "CameraTest";
    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        //处理图片
        //裁剪图片
        img = (ImageView) findViewById(R.id.iv);
        initView();
    }

    private void initView() {
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) return;

        final String sdPath = Environment.getExternalStorageDirectory() + File.separator + "PIC" + File.separator;
        File path = new File(sdPath);
        if (!path.exists()) path.mkdirs();

        Button camera = (Button) findViewById(R.id.camera);
        camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                String filePath = System.currentTimeMillis() + ".jpg";
                File imgFile = new File(sdPath, filePath);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                Uri uri;
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//                    uri = FileProvider.getUriForFile(context, context.getPackageName().concat(".provider"), imgFile);
////                    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
////                    StrictMode.setVmPolicy(builder.build());
////                    uri = Uri.fromFile(imgFile);
//                } else {
//                uri = Uri.fromFile(imgFile);
//                }
                uri = Uri.fromFile(imgFile);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
                startActivityForResult(intent, 0);
            }
        });
    }


    Bitmap photo;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == 0) {

            Log.e(TAG, "onActivityResult: 照片获取成功" );

            //两种方式 获取拍好的图片
            Uri uri = data.getData();
            Bundle bundle = data.getExtras();
            if (uri != null || bundle != null) { //防止没有返回结果
                photo = BitmapFactory.decodeFile(uri.getPath()); //拿到图片
                if (photo == null) {
                    if (bundle != null) {
                        photo = (Bitmap) bundle.get("data");
                    } else {
                        Toast.makeText(getApplicationContext(), "找不到图片", Toast.LENGTH_SHORT).show();
                    }
                }

                img.setImageBitmap(photo);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
